#!/usr/bin/env python
# -*- coding:utf-8 -*-

import sys
import os

try:
    sys.path.append(r'D:\Develop\GitRepos\BIM\blender\2018\03')
    import bpy
    import bmesh
    import ut
    import importlib
    importlib.reload(ut)
    print(ut)
except:
    pass

# Must start in object mode
# Script will fail if scene is empty
bpy.ops.object.mode_set(mode = 'OBJECT')
bpy.ops.object.select_all(action = 'SELECT')
bpy.ops.object.delete()

# 创建一个立方体并进入编辑模式
bpy.ops.mesh.primitive_cube_add(radius = 0.5, location = (-3, 0, 0))
bpy.ops.object.mode_set(mode = 'EDIT')

bpy.ops.mesh.select_mode(type = "FACE")

# 保存对mesh数据块的引用
mesh_datablock = bpy.context.object.data

# 创建bmesh对象
bm = bmesh.from_edit_mesh(mesh_datablock)

bpy.ops.mesh.select_all(action = 'DESELECT')

# 选择一个面
bm.faces.ensure_lookup_table()
bm.faces[0].select = True
bpy.ops.transform.rotate(value = 0.3, axis = (0, 1, 0))

## Select an edge
#bm.edges.ensure_lookup_table()
#bm.edges[7].select = True

## Select a vertex
#bm.verts.ensure_lookup_table()
#bm.verts[5].select = True

bpy.ops.object.mode_set(mode='OBJECT')

# Create a cube and pull an edge along the y-axis
bpy.ops.mesh.primitive_cube_add(radius=0.5, location=(0, 0, 0))
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action="DESELECT")
bm = bmesh.from_edit_mesh(bpy.context.object.data)
bm.edges.ensure_lookup_table()
bm.edges[4].select = True
bpy.ops.transform.translate(value = (0, 0.5, 0))
bpy.ops.object.mode_set(mode='OBJECT')


# Create a cube and pull a vertex 1 unit
# along the y and z axes
# Create a cube and pull an edge along the y-axis
bpy.ops.mesh.primitive_cube_add(radius=0.5, location=(3, 0, 0))
bpy.ops.object.mode_set(mode='EDIT')
bpy.ops.mesh.select_all(action="DESELECT")
bm = bmesh.from_edit_mesh(bpy.context.object.data)
bm.verts.ensure_lookup_table()
bm.verts[3].select = True
bpy.ops.transform.translate(value = (0, 1, 1))
bpy.ops.object.mode_set(mode='OBJECT')